package com.example.phone;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private String number;
    private EditText editText;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=(EditText) findViewById(R.id.btn2);
        button = (Button) findViewById(R.id.btn3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                number=editText.getText().toString().trim();
                search();
            }
        });
    }
    VResponse vResponse;
    private void search(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient=new OkHttpClient();
                Request request =new Request.Builder().url("https://apis.juhe" +
                        ".cn/mobile/get?phone="+number+"&key=a3eaf52975c81e6156b4d28abe3e638c").get().build();
                Call call =okHttpClient.newCall(request);
                try {
                    Response response=call.execute();
                    Gson gson =new Gson();
                    vResponse =gson.fromJson(response.body().string(),VResponse.class);
                  Log.e("response",vResponse.getResult().getCity());
                    uiText();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void uiText(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new AlertDialog.Builder(MainActivity.this).setMessage(vResponse.getResult().getCity())
                        .setPositiveButton("确定",null)
                        .show()
                ;
            }
        });

    }
}

